package com.iscreen.downloader.settings;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.iscreen.utils.exceptions.IScreenException;
import com.iscreen.utils.settings.Settings;

public class DownloaderSettings extends Settings {

	private final static String PROPERTIES_PACKAGE = DownloaderSettings.class
			.getPackage().getName();

	private final static String DOWNLOADER_PROPERTIES_FILE = "downloader.properties";

	private final static String ENTRY_BASE_STRUCTURES_URL = "BASE_STRUCTURES_URL";
	private final static String ENTRY_DOWNLOAD_TARGET_PATH = "DOWNLOAD_TARGET_PATH";

	private final static String PREFIX_TABLE_DEF = "TABLE_DEF";
	
	private static DownloaderSettings settings = new DownloaderSettings();

	private String baseStructuresURL;
	private String downloadTargetPath;
	private Map<String,String> tableDefs;
	
	private DownloaderSettings()
	{
		super(getDownloaderPropertiesFileName());
	}

	public static DownloaderSettings getInstance() throws IScreenException {
		return (DownloaderSettings) settings.getInitializedInstance();
	}
	
	private static String getDownloaderPropertiesFileName() {
		return PROPERTIES_PACKAGE.replace(".", "/") + "/" + DOWNLOADER_PROPERTIES_FILE;
	}

	private void initTableDefs()
	{
		tableDefs = new HashMap<String, String>();
		for (Entry<Object, Object> entry : properties.entrySet())
		{
			String key = (String) entry.getKey();
			if (key.startsWith(PREFIX_TABLE_DEF))
			{
				String value = (String) entry.getValue();
				tableDefs.put(key, value);
			}
		}
	}
	@Override
	protected void initSettings() {
		baseStructuresURL = properties.getProperty(ENTRY_BASE_STRUCTURES_URL);
		downloadTargetPath = properties.getProperty(ENTRY_DOWNLOAD_TARGET_PATH);
		initTableDefs();
	}

	public String getBaseStructuresURL() {
		return baseStructuresURL;
	}

	public String getDownloadTargetPath() {
		return downloadTargetPath;
	}

	public Map<String, String> getDownloadMappings()
	{
		return tableDefs;
	}
}
